package com.icesoft.xsnow.upms.controller;

import com.icesoft.xsnow.framework.api.reponses.R;
import com.icesoft.xsnow.framework.controller.BaseController;
import com.icesoft.xsnow.upms.api.po.SysUser;
import com.icesoft.xsnow.upms.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @program: xsnow
 * @description: 演示用例
 * @author: xuefeng.gao
 * @create: 2019-05-16 13:40
 **/
@RestController
@Validated
@RequestMapping(value = "/test", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(tags = {"Test"}, description = "[演示用例]restful风格API接口")
public class TestController extends BaseController {
    @Autowired
    private SysUserService userService;

    @ApiOperation("查询单个用户")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "path") })
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return success(userService.getById(id));
    }


    @ApiOperation("分页查询所有用户")
    @GetMapping("/page")
    public R page() {
        return success(userService.page(this.getPage()));
    }


    @ApiOperation("新增用户")
    @PreAuthorize("@pms.hasPermission('tb:user:add')")
    @PostMapping
    public R save(@Valid @RequestBody SysUser SysUser) {
        return success(userService.save(SysUser));
    }


    @ApiOperation("编辑用户")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('tb:user:edit')")
    public R update(@Valid @RequestBody SysUser SysUser) {
        return success(userService.updateById(SysUser));
    }


    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('tb:user:del')")
    public R removeById(@PathVariable Integer id) {
        return success(userService.removeById(id));
    }

}
